package sandbox.patterns.quantity;

/**
 * Understands denominations of a quantity
 */
public abstract class Unit<U extends Unit<U>> {

    private double scale;
    private double translation;
    private String name;

    protected Unit(double translation, double scale, String name) {
        this.translation = translation;
        this.scale = scale;
        this.name = name;
    }

    protected Unit(double scale, String name) {
        this(0f, scale, name);
    }

    @SuppressWarnings(value = "unchecked")
    public Quantity<U> s(double n) {
        return new Quantity<U>(n, (U) this);
    }

    public double from(double amount, U other) {
        return ((amount - other.translation) * other.scale / this.scale) + this.translation;
    }

    public String getName() {
        return name;
    }
}
